package com.heima.common.config;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties(SnowflakeProperties.class)
public class SnowflakeAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public IdentifierGenerator customIdGenerator(SnowflakeProperties properties) {
        Sequence sequence = new Sequence(properties.getWorkerId(), properties.getDatacenterId());
        log.info("Snowflake ID Generator initialized: datacenterId={}, workerId={}",
                properties.getDatacenterId(), properties.getWorkerId());
        return entity -> sequence.nextId();
    }
}
